package com.pr.makfilm.user.pages.room.details;

import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.tabbedPanel.BootstrapTabbedPanel;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.api.UserApiKeys;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.pages.AbstractUserPage;
import com.pr.makfilm.user.pages.room.details.cmp.RoomDataTab;
import com.pr.makfilm.user.pages.room.details.cmp.RoomDesignTab;
import com.pr.makfilm.user.pages.room.details.cmp.RoomEventWindow;
import com.pr.makfilm.user.pages.room.details.cmp.RoomEventsTab;
import com.pr.makfilm.user.pages.room.list.RoomListPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 14.10.13
 * Time: 23:55
 * To change this template use File | Settings | File Templates.
 */
@MountPath(UserApiMappings.ROOM_DETAILS_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class RoomDetailsPage extends AbstractUserPage
{
   @SpringBean
   private IRoomService roomService;

   private Room room;

   private AjaxLink newEvent;

   private RoomEventWindow window;

   public RoomDetailsPage(PageParameters parameters)
   {
      super(parameters);
      room = getRoom(parameters);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      List<ITab> tabs = new ArrayList<ITab>();
      tabs.add(new AbstractTab(new ResourceModel("room.data.tab"))
      {
         @Override
         public WebMarkupContainer getPanel(String panelId)
         {
            return new RoomDataTab(panelId, new PropertyModel<Room>(RoomDetailsPage.this, "room"));
         }
      });
      tabs.add(new AbstractTab(new ResourceModel("room.design.tab"))
      {
         @Override
         public WebMarkupContainer getPanel(String panelId)
         {
            return new RoomDesignTab(panelId, new PropertyModel<Room>(RoomDetailsPage.this, "room"));
         }
      });
      tabs.add(new AbstractTab(new ResourceModel("room.events.tab"))
      {
         @Override
         public WebMarkupContainer getPanel(String panelId)
         {
            return new RoomEventsTab(panelId, new PropertyModel<Room>(RoomDetailsPage.this, "room"));
         }
      });
      add(new BootstrapTabbedPanel("tabs", tabs));

      add(window = new RoomEventWindow("window", new PropertyModel<Room>(this, "room")));
      add(newEvent = new AjaxLink<Void>("newEvent")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.show(target);
         }
      });
   }

   private Room getRoom(PageParameters parameters)
   {
      String sid = parameters.get(UserApiKeys.SID_KEY).toString();
      if (StringUtils.isNotBlank(sid))
      {
         Room obj = roomService.getBySid(sid);
         if (obj == null)
         {
            throw new RestartResponseAtInterceptPageException(RoomListPage.class);
         }
         return obj;
      }
      else
      {
         throw new RestartResponseAtInterceptPageException(RoomListPage.class);
      }
   }
}
